package com.ujs.eduservice.controller.admin;


import com.ujs.commonutils.constant.PublishStatus;
import com.ujs.commonutils.constant.R;
import com.ujs.eduservice.entity.EduCourse;
import com.ujs.eduservice.entity.vo.CourseInfoVo;
import com.ujs.eduservice.entity.vo.CoursePublishVo;
import com.ujs.eduservice.entity.vo.CourseQueryVo;
import com.ujs.eduservice.service.EduCourseService;
import com.ujs.servicebase.exception.GuliException;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Map;

/**
 * <p>
 * 课程 前端控制器
 * </p>
 *
 * @author testjava
 * @since 2023-08-07
 */
@RestController
@RequestMapping("/eduservice/course")
@Api(tags = "课程管理api")
public class EduCourseController {
    @Autowired
    private EduCourseService courseService;

    @ApiOperation("课程列表功能")
    @PostMapping("/getCourseList/{current}/{size}")
    public R getCourseList(@PathVariable("current") Long current,
                           @PathVariable("size") Long size,
                           @RequestBody CourseQueryVo courseQueryVo) {
        Map<String, Object> map = courseService.getCourseList(current, size, courseQueryVo);
        return R.ok().data("map", map);
    }

    @ApiOperation("删除课程，同时删除课程描述、章节、小节、视频")
    @DeleteMapping("/deleteCourse/{courseId}")
    public R deleteCourse(@PathVariable("courseId") String courseId) {
        try {
            courseService.deleteCourseAllInfo(courseId);
            return R.ok();
        } catch (GuliException e) {
            e.printStackTrace();
            return R.error().message(e.getMsg());
        }
    }

    @PostMapping("/addCourseInfo")
    @ApiOperation("添加课程")
    public R addCourseInfo(@RequestBody CourseInfoVo vo) {
        String id = courseService.saveCourseInfo(vo);
        return R.ok().data("courseId", id);
    }

    @GetMapping("/getCourseInfo/{courseId}")
    @ApiOperation("根据课程id，获取课程基本信息")
    public R getCourseInfo(@PathVariable("courseId") String courseId) {
        try {
            CourseInfoVo courseInfoVo = courseService.getCourseInfo(courseId);
            return R.ok().data("course", courseInfoVo);
        } catch (GuliException e) {
            e.printStackTrace();
            return R.error().message(e.getMsg());
        }
    }

    @PostMapping("/updateCourseInfo")
    @ApiOperation("更新课程信息")
    public R updateCourseInfo(@RequestBody CourseInfoVo courseInfoVo) {
        try {
            courseService.updateCourseInfo(courseInfoVo);
            return R.ok();
        } catch (GuliException e) {
            e.printStackTrace();
            return R.error().message(e.getMsg());
        }
    }

    @ApiOperation("根据课程id，查询课程全部信息")
    @GetMapping("/getPublishCourseInfo/{courseId}")
    public R getPublishCourseInfo(@PathVariable("courseId") String courseId) {
        CoursePublishVo course = courseService.getPublishCourseInfo(courseId);
        return R.ok().data("course", course);
    }

    @ApiOperation("课程最终发布，修改status")
    @GetMapping("/publishCourse/{id}")
    public R publishCourse(@PathVariable String id) {
        EduCourse eduCourse = new EduCourse();
        eduCourse.setId(id);
        eduCourse.setStatus(PublishStatus.PUBLISHED);
        courseService.updateById(eduCourse);
        return R.ok();
    }
}

